<%
' +----------------------------------------------------------------------
' | POPASP [ ASP MVC ]
' +----------------------------------------------------------------------
' | Copyright (c) 2016 http://popasp.com All rights reserved.
' +----------------------------------------------------------------------
' | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
' +----------------------------------------------------------------------
' | Author: popasp <1737025626@qq.com>
' +----------------------------------------------------------------------
Class POPASP_MVC
	'在本类中不能使用array函数
	public mvc_dir,config		' dict对象
	public dG_,dN_		'对应获取函数G_、N_
	Public dClass		' 存储实例化的类
	Public dDict		' 存储实例化的Dictionary对象，可自动销毁
	Public reg		' Server.createObject("Scriptint.FileSystemObject")、RegExp对象
	Public Dict,[Arr],[String]	' 具体的类 Application、DICTIONARY、STRING、ARRAY
	Private is_init_popasp
	Private isExit
	
	public Sub init
		if not is_init_popasp Then
			'加载 POPASP_STRING 类
			
			
			set [String] = import("POPASP_STRING")			
			
			'加载 POPASP_DICTIONARY 类
			set Dict = import("POPASP_DICTIONARY")
			
			'加载 POPASP_ARR 类
			set [Arr] = import("POPASP_ARR")			
		
			is_init_popasp = true
		end If
	End Sub
	
	' 使用该方法与D_函数创建的Dictionary对象，程序结束时均可自动销毁
	Public Function CreateDict()
		dim count
		count = dDict.Count
		set dDict( count + 1 ) = Server.CreateObject("Scripting.Dictionary")
		Set CreateDict = dDict( count + 1 )
	End Function
	
	public function import_config( file_path )
		import_config = import("POPASP_CONFIG").import_config(file_path)
	End function
  
	Private Sub Class_Initialize
		'Application.Contents.RemoveAll		
		set dDict = Server.CreateObject("Scripting.Dictionary")
		set dClass = CreateDict()
		set dG_ = CreateDict()
		set dN_ = CreateDict()
		
		set reg = new RegExp
		
		set config = CreateDict()
		reg.Global = true : reg.IgnoreCase = true : reg.MultiLine = true
		reg.Pattern = ""
		is_init_popasp = false
	End Sub	
	
	Private Sub Class_Terminate	
		on error resume next
		isExit = false
		dim key
		set reg = nothing
		set config = nothing
		set Dict = nothing
		set dG_ = nothing
		call quit
	End Sub
	
	'是否是POPASP_ 开头的类名
	Function isPopasp( className )
		isPopasp = ( UCase(Left( className , 7)) = "POPASP_" )
	End Function
  
	'获取某个参数
	Public Property Get getConfig(byval key)
		dim arr,i,str
		if isNull(key) Then
			set getConfig = config		
		Else 			
			if inStr( key , "." ) > 0 Then				
				arr = split(key,".")
				str = "POP_MVC.config"				
				for i = 0 to ubound(arr)
					str = str & "(""" & arr(i) & """)"
				next
				Execute "getConfig = " & str			
			elseif config.Exists( key ) Then				
				If isObject( config(key) ) Then				
					set getConfig = config( key )
				else
					getConfig = POP_MVC.config(key)
				End if
			Else
				getConfig = Empty
			End If			
		End if    
	End Property
	
	Function isScalar( byref opt )
		dim label : label = TypeName(opt)
		isScalar = (label = "String" OR label = "Integer" OR label = "Long" OR label = "Double" OR label = "Boolean")
	End Function
	
	'获取文件内容
	Function file_get_contents( ByVal filePath )
		on error resume next
		dim startTime : startTime = timer()
		Dim o_strm, tmpStr,p
		p = Server.MapPath(filePath)
		  Set o_strm = Server.CreateObject("ADODB.Stream")
		  With o_strm
			.Type = 2
			.Mode = 3
			.Open
			.LoadFromFile p
			.Charset = "UTF-8"
			.Position = 2
			tmpStr = .ReadText
			.Close
		  End With
		  Set o_strm = Nothing
		  tmpStr = Replace(tmpStr, "^\uFEFF", "" , 1,1,0)
		file_get_contents = tmpStr
	End Function
	
	' 给文件写入内容，如果文件不存在，则尝试创建
	Function file_put_contents(filePath,content)
		on error resume next
		dim oStream , startTime : startTime = timer()
		content = Replace(content, "^\uFEFF", "" , 1,1,0)
		
		filePath = Replace( filePath, "/" , "\" )
		file_put_contents = CreateFolder( Left(filePath,InstrRev(filePath,"\")-1) )

		if file_put_contents Then
			Set oStream = Server.CreateObject("ADODB.Stream")
			With oStream
				.Type = 2	'以本模式读取
				.mode = 3
				.Open()
				.Charset = "utf-8"
				.Position = .Size

				.WriteText(content)
				.SaveToFile realPath(filePath),2

				.Flush
				.Close
			End With
			Set oStream=Nothing

			file_put_contents = iif((Err.Number = 0),True,False)		
		end if
	end Function
	
	
	' 获取绝对路径
	Function realPath( path )
		if inStr(path,":") > 0 then
			realPath = path
		else
			realPath =Server.mapPath( Replace(  path  ,"/" , "\" ) )
		end if
	End Function
	
	'//自动生成文件夹路径
	Function CreateFolder( ByVal path )
		Dim i , folder
		CreateFolder = True
		'//看看是否已经存在目录
		path = realPath(path)
		If fso.FolderExists(path) Then
			Exit Function
		Else
			path = Replace(path,"/","\")
			path = Split(path,"\")
		For i = 0 To Ubound(path)
			folder = folder & path(i) & "\"	
			If Not fso.folderExists(folder) Then
				fso.CreateFolder(folder)
			End If
		Next		 
			CreateFolder = iif((Err.Number = 0),True,False)
		End If
	End Function
  
	' 从asp文件中取得内容，并且删除第一个与最后一个出现在asp代码标记
	Function asp_get_contents ( file )
		
		dim str,start_,end_
		str		= file_get_contents(file)
		start_	= InStr(str,"<" & "%")
		end_	= InStrRev( str,"%" & ">" )
		
		str= mid(str,start_ + 2,end_-4) 
		
		asp_get_contents = str
	End Function
  
	' 引入类文件或控制器文件
	Function import(class_name)
		on error resume next

		'将每个生成的对象都保存到dClass中，如果dClass存在这个对象，则直接取出
		if dClass.Exists(class_name) Then
			set import = dClass(class_name) : exit Function
		end if
		
		
		
		'如果不存在，则第一次实例化该对象，达到单例模式的目的
		execute get_import_asp(class_name)
		if err.number <> 0 Then
			if not is_empty(C("APP_DEBUG")) Then
				call [Exit]( class_name & "类中代码有误:" & err.number & "," & err.description)
			end if
		end if
		
		Execute "Set dClass(""" & class_name & """) = New " & class_name

		set import = dClass(class_name)
	End Function
	
	Sub [exit]( byref str )
		Response.write str
		call quit
	End Sub
	
	Sub quit
		err.clear
		on error resume next
		dim key,key2	
		
		if isObject( dClass ) then
			for each key in dClass
				if isObject( dClass(key) ) then
					set dClass(key) = Nothing
				end if	
			next
			set dClass = Nothing
		end if
		
		for each key in dDict
			set dDict(key) = nothing
		next
		set dDict = nothing
		
		if isExit then
			Response.end
		end if
	End sub
	
	Public Function Logo()
		Logo = file_get_contents( mvc_dir & "Tpl/logo.txt" )
	End Function
	
	Function get_import_asp(class_name)		
		
	
		if isPopasp(class_name) then	'如果是框架的类
			get_import_asp = asp_get_contents( get_mvc_class_path(class_name) )
		else	'不是框架的类，则应该提供路径
			get_import_asp = asp_get_contents( class_name )
		end if
	End Function
	
	Private Function get_mvc_class_path(class_name)		
		get_mvc_class_path = mvc_dir & LCase(class_name) & ".class.asp"		
	End Function
	
	' 计算个数
	Public Function count( var )
		if isArray(var) Then
			count = ubound(var) + 1
		else
			select case TypeName(var)
				case "String"
					count = Len(var)
				case "Integer","Long","Single","Double","Currency","Decimal","Date","Boolean"
					count = 1
				case "Dictionary","Files","IVariantDictionary,IRequestDictionary"
					count = var.Count
				case "Recordset"
					count = var.RecordCount
				case "ISessionObject"
					count = var.Contents.Count
				case "Empty","Null","Unknown","Nothing","Error"
					count = 0
				case else
					count = 0
			End Select		
		End If
	End Function 
End Class
%>